# app.py
import streamlit as st
import os
import importlib

pages_dir = "pages"
pages = {}

def extend_pages(pages_dir):
    pages[pages_dir] = []
    for filename in os.listdir(pages_dir):
        if filename.endswith(".py") and not filename.startswith("__"):
            path = os.path.join(pages_dir, filename)
            module_name = f"{pages_dir}.{filename[:-3]}"
            module = importlib.import_module(module_name)
            page_config = getattr(module, "PAGECONFIG", None)
            # 使用文件名作为唯一标识符
            if page_config:
                pages[pages_dir].append(st.Page(module.run, title=page_config["title"], icon=page_config.get("icon", None), url_path=path))
            else:
                pages[pages_dir].append(st.Page(module.run, title=filename[:-3], url_path=path))

extend_pages("pages")
extend_pages("private")
pg = st.navigation(pages)
pg.run()
